﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.WpfShell.ServiceRef;
using SmartSpace.WpfShell.Modules.Dynamic.Util;

namespace SmartSpace.WpfShell.Modules.Dynamic
{
    /// <summary>
    /// Interaction logic for UpdateTemplateView.xaml
    /// </summary>
    public partial class UpdateTemplateView : Page
    {
        private Guid _workflowId { get; set; }
        private Guid _activityId { get; set; }
        private Guid _dataId { get; set; }
        private DetailData _detailData;

        public UpdateTemplateView(Guid workflowId, Guid activityId, Guid dataId)
        {
            InitializeComponent();

            _workflowId = workflowId;
            _activityId = activityId;
            _dataId = dataId;

            this.Loaded += new RoutedEventHandler(UpdateTemplateView_Loaded);
        }

        void UpdateTemplateView_Loaded(object sender, RoutedEventArgs e)
        {
            ServiceRef.SsWebService client = App.GetProxy();
            client.GetDetailMetadataCompleted += new GetDetailMetadataCompletedEventHandler(client_GetDetailMetadataCompleted);
            client.GetDetailMetadataAsync(this._workflowId, this._activityId);
        }

        void client_GetDetailMetadataCompleted(object sender, GetDetailMetadataCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                #region 更新按钮权限
                if (e.Result.Data.CanModify)
                {
                    this._ctlBtnSave.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    this._ctlBtnSave.Visibility = System.Windows.Visibility.Collapsed;
                }
                #endregion

                #region 删除按钮权限
                if (e.Result.Data.CanRemove)
                {
                    this._ctlBtnRemove.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    this._ctlBtnRemove.Visibility = System.Windows.Visibility.Collapsed;
                }
                #endregion

                foreach (FieldMetadata item in e.Result.Data.Fields)
                {
                    if (!item.IsHidden)
                    {
                        TextBlock label = new TextBlock();
                        label.Margin = new Thickness(5);
                        label.FontSize = 14;
                        label.Text = item.Caption;
                        _ctlContainer.Children.Add(label);

                        FrameworkElement input = UIBuilder.CreateFieldControl(item);
                        _ctlContainer.Children.Add(input);
                    }

                }

                ServiceRef.SsWebService client = App.GetProxy();
                client.GetDataCompleted += new GetDataCompletedEventHandler(client_GetDataCompleted);
                client.GetDataAsync(_workflowId, _activityId, this._dataId);
            }
        }

        void client_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                _detailData = e.Result.Data;

                if (_detailData.Events== null || _detailData.Events.Length == 0)
                {
                    _ctlPanelEvent.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    _ctlPanelEvent.Visibility = System.Windows.Visibility.Visible;
                }

                this.DataContext = e.Result.Data;
            }
        }

        private void _ctlBtnSave_Click(object sender, RoutedEventArgs e)
        {
            ServiceRef.SsWebService client = App.GetProxy();
            client.SaveCompleted += new SaveCompletedEventHandler(client_SaveCompleted);
            client.SaveAsync(_detailData);
        }

        void client_SaveCompleted(object sender, SaveCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                MessageBox.Show("保存成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show(e.Result.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void _ctlBtnSubmit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void _ctlBtnBack_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}
